package me.zhengjie.pm.projectbase.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import me.zhengjie.annotation.FieldMeta;
import me.zhengjie.hr.employeebase.domain.EmployeeBase;
import me.zhengjie.org.centerbase.domain.CenterBase;
import me.zhengjie.org.departmentbase.domain.DepartmentBase;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Entity
@Data
@Table(name="project_base")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ProjectBase implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    /** 项目类型 */
    @Column(name = "project_type")
    @FieldMeta(name = "项目类型")
    private String projectType;

    /** 项目名称 */
    @Column(name = "project_name")
    @FieldMeta(name = "项目名称")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(project_name), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String projectName;

    /** 主管部门 */
    @Column(name = "department_id")
    @FieldMeta(name = "主管部门ID")
    private Integer departmentId;
    @OneToOne
    @JoinColumn(name = "department_id", insertable = false, updatable = false, nullable=true)
    private DepartmentBase departmentBase;

    /** 主管中心 */
    @Column(name = "center_id")
    @FieldMeta(name = "主管中心ID")
    private Integer centerId;
    @OneToOne
    @JoinColumn(name = "center_id", insertable = false, updatable = false, nullable=true)
    private CenterBase centerBase;

    /** 主管部门领导 */
    @Column(name = "department_leader")
    @FieldMeta(name = "主管部门领导ID")
    private Integer departmentLeaderId;
    @OneToOne
    @JoinColumn(name = "department_leader", insertable = false, updatable = false, nullable=true)
    private EmployeeBase departmentLeaderBase;

    /** 主管所领导 */
    @Column(name = "institute_leader")
    @FieldMeta(name = "主管所领导ID")
    private Integer instituteLeaderId;
    @OneToOne
    @JoinColumn(name = "institute_leader", insertable = false, updatable = false, nullable=true)
    private EmployeeBase instituteLeaderBase;

    /** 项目编号/核算码 */
    @Column(name = "project_no")
    @FieldMeta(name = "项目编号/核算码")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(project_no), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String projectNo;

    /** 密级 */
    @Column(name = "secret_level")
    @FieldMeta(name = "密级")
    private String secretLevel;

    /** 重要程度 */
    @Column(name = "importance")
    @FieldMeta(name = "重要程度")
    private String importance;

    /** 总体单位 */
    @Column(name = "main_company")
    @FieldMeta(name = "总体单位")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(main_company), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String mainCompany;

    /** 研制内容 */
    @Column(name = "development_contents")
    @FieldMeta(name = "研制内容")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(development_contents), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String developmentContents;

    /** 项目工作文档位置 */
    @Column(name = "doc_path")
    @FieldMeta(name = "项目工作文档位置")
    private String docPath;

    /** 项目代码位置 */
    @Column(name = "code_path")
    @FieldMeta(name = "项目代码位置")
    private String codePath;

    /** 项目开始时间 */
    @Column(name = "start_time")
    @FieldMeta(name = "项目开始时间")
    private Timestamp startTime;

    /** 项目结束时间 */
    @Column(name = "end_time")
    @FieldMeta(name = "项目结束时间")
    private Timestamp endTime;

    /** 项目所属计划类型 */
    @Column(name = "project_plan_type")
    @FieldMeta(name = "项目所属计划类型")
    private String projectPlanType;

    /** 主要完成形式/成果 */
    @Column(name = "achievement")
    @FieldMeta(name = "主要完成形式/成果")
    private String achievement;

    /** 计划开始时间 */
    @Column(name = "plan_start_time")
    @FieldMeta(name = "计划开始时间")
    private Timestamp planStartTime;

    /** 计划完成时间 */
    @Column(name = "plan_finish_time")
    @FieldMeta(name = "计划完成时间")
    private Timestamp planFinishTime;

    /** 计划依据 */
    @Column(name = "plan_basis")
    @FieldMeta(name = "计划依据")
    private String planBasis;

    /** 项目状态 */
    @Column(name = "project_state")
    @FieldMeta(name = "项目状态")
    private String projectState;

    public void copy(ProjectBase source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}